using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class UI_CraftWindow : MonoBehaviour
{
    // 序列化字段，用于在编辑器中设置物品名称的文本组件
    [SerializeField] private TextMeshProUGUI itemName;
    // 序列化字段，用于在编辑器中设置物品描述的文本组件
    [SerializeField] private TextMeshProUGUI itemDescription;
    // 序列化字段，用于在编辑器中设置物品图标的图像组件
    [SerializeField] private Image itemIcon;
    // 序列化字段，用于在编辑器中设置材料图像的数组
    [SerializeField] private Image[] materialImage;
    // 序列化字段，用于在编辑器中设置制造按钮
    [SerializeField] private Button craftButton;

    // 设置制造窗口
    public void SetupCraftWindow(ItemData_Equipment _data)
    {
        // 移除制造按钮的所有监听器
        craftButton.onClick.RemoveAllListeners();

        // 清除所有材料图像和文本的颜色
        for (int i = 0; i < materialImage.Length; i++)
        {
            materialImage[i].color = Color.clear;
            materialImage[i].GetComponentInChildren<TextMeshProUGUI>().color = Color.clear;
        }

        // 设置每个材料图像和文本
        for (int i = 0; i < _data.craftingMaterials.Count; i++)
        {
            materialImage[i].sprite = _data.craftingMaterials[i].data.icon;
            materialImage[i].color = Color.white;

            TextMeshProUGUI materialSlotText = materialImage[i].GetComponentInChildren<TextMeshProUGUI>();
            materialSlotText.text = _data.craftingMaterials[i].stackSize.ToString();
            materialSlotText.color = Color.white;
        }

        // 设置物品图标、名称和描述
        itemIcon.sprite = _data.icon;
        itemName.text = _data.itemName;
        itemDescription.text = _data.GetDescription();

        // 为制造按钮添加点击事件监听器
        craftButton.onClick.AddListener(() =>
        {
            Inventory.instance.CanCraft(_data, _data.craftingMaterials);
        });
    }
}